from django.test import TestCase, Client
from django.conf import settings
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'tongjiPlatform.settings'


# Create your tests here.
class SpiderTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.register('clj', 'Fu.2017%Hao', 'clj@qq.com')
        self.login(self.client, 'clj', 'Fu.2017%Hao')

    def tearDown(self):
        self.logout(self.client)

    def runTest(self):
        self.test_index()
        # self.test_project_info()
        # self.test_show_projectinfo()
        # self.test_list_spiders()
        # self.test_start_spider()
        # self.test_cancel_spider()

    def register(self, username, password, email=''):
        """
        注册
        :return:
        """
        client = Client()
        response = client.post('/auths/register/', {
            'username': username,
            'password': password,
            'email': email})
        self.assertEqual(response.status_code, 200, bytes.decode(response.content))
        print('注册成功')

    def login(self, client, username, password):
        """
        登录
        :return:
        """
        response = client.post('/auths/login/', {
            'username': username,
            'password': password
        })
        self.assertEqual(response.status_code, 200, bytes.decode(response.content))
        print('登录成功')

    def logout(self, client):
        """
        登出
        :return:
        """
        response = client.post('/auths/logout/')
        self.assertEqual(response.status_code, 200, bytes.decode(response.content))
        print('登出成功')

    def test_index(self):
        # {"node_name": "impala01", "status": "ok", "projects": ["openCourse", "default"]}
        response = self.client.get('/crawler/')
        self.assertEqual(response.status_code, 200, bytes.decode(response.content))
        print("---test_index响应状态码:%s---响应内容：%s---" % (response.status_code, response.content))

    def test_show_projectinfo(self):
        content = {'project': 'zjbScrpay'}
        response = self.client.get('/crawler/project/%s' % (content['project']), data=content)
        self.assertEqual(response.status_code, 200, bytes.decode(response.content))
        print(bytes.decode(response.content))

    def test_project_info(self):
        content = {'project': 'zjbScrpay'}
        response = self.client.get('/crawler/project/%s' % (content['project']), data=content)
        self.assertEqual(response.status_code, 200, bytes.decode(response.content))
        print("---test_project_info响应状态码:%s---响应内容：%s---" % (response.status_code, response.content))

    def test_list_spiders(self):
        content = {'version': '1528774791', 'project': 'zjbScrpay'}
        response = self.client.get('/crawler/listspiders/%s/%s' % (content['project'], content['version']),
                                   data=content)
        self.assertEqual(response.status_code, 200, bytes.decode(response.content))
        print("---test_list_spiders响应状态码:%s---响应内容：%s---" % (response.status_code, response.content))

    def test_start_spider(self):
        content = {
            'dbForm': 'MongoDB',
            'dbName': 'dbName',
            'dbType': 'mongodb',
            'crawlerAmount': '1',
            'crawlerDelete': 'true',
            'dbUri': '123',
            'project': 'zjbScrpay',
            'spider': 'zhejiang',
            'storeType': 'storeType'
        }
        # import json
        # content = json.loads(content)
        response = self.client.post('/crawler/schedule', data=content)
        self.assertEqual(response.status_code, 200, bytes.decode(response.content))
        print("---test_start_spider响应状态码:%s---响应内容：%s---" % (response.status_code, response.content))

    def test_cancel_spider(self):
        content = {'job': 'Scrapy_jobs', 'project': 'zjbScrpay'}
        response = self.client.post('/crawler/cancel', data=content)
        self.assertEqual(response.status_code, 200, bytes.decode(response.content))
        print("---test_cancel_spider响应状态码:%s---响应内容：%s---" % (response.status_code, response.content))
